1 module std.vita.netinet_in;
2 version(PSVita):
3 import core.stdc.config;
4 nothrow @nogc pure:
5 
6 T _howmany(T)(T x, T y) =>	(((x) + ((y) - 1)) / (y));
7 
8 alias  __fd_mask = c_ulong;
9 alias fd_mask = __fd_mask;
10 
11 
12 enum _NFDBITS = cast(int)__fd_mask.sizeof * 8; /* bits per mask */
13 alias NFDBITS = _NFDBITS;
14 
15 
16 struct fd_set {
17 	__fd_mask[_howmany(FD_SETSIZE, _NFDBITS)]	__fds_bits;
18 }
19 
20 /*
21  * Copyright (c) 2016, 2017, 2018 vitasdk
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions
26  * are met:
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  * 2. Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in the
31  *    documentation and/or other materials provided with the distribution.
32  * 3. Neither the name of the copyright holder nor the names of its
33  *    contributors may be used to endorse or promote products derived from
34  *    this software without specific prior written permission.
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37  * "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
42  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
43  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
44  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
45  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  */
48 
49 
50 import std.stdint;
51 import std.vita.socket;
52 
53 extern(C):
54 
55 /** Net Protocols */
56 enum SceNetProtocol {
57 	SCE_NET_IPPROTO_IP      = 0,
58 	SCE_NET_IPPROTO_ICMP    = 1,
59 	SCE_NET_IPPROTO_IGMP    = 2,
60 	SCE_NET_IPPROTO_TCP     = 6,
61 	SCE_NET_IPPROTO_UDP     = 17,
62 	SCE_NET_SOL_SOCKET      = 0xFFFF
63 }
64 
65 
66 enum IPPROTO_IP = SceNetProtocol.SCE_NET_IPPROTO_IP;
67 enum IPPROTO_ICMP = SceNetProtocol.SCE_NET_IPPROTO_ICMP;
68 enum IPPROTO_IGMP = SceNetProtocol.SCE_NET_IPPROTO_IGMP;
69 enum IPPROTO_TCP = SceNetProtocol.SCE_NET_IPPROTO_TCP;
70 enum IPPROTO_UDP = SceNetProtocol.SCE_NET_IPPROTO_UDP;
71 
72 alias in_addr_t = uint32_t ;
73 alias in_port_t = uint16_t ;
74 
75 struct in_addr {
76 	in_addr_t s_addr;
77 }
78 
79 struct sockaddr_in {
80 	uint8_t			sin_len;
81 	sa_family_t		sin_family;
82 	in_port_t		sin_port;
83 	in_addr		sin_addr;
84 	in_port_t		sin_vport;
85 	char[6]			sin_zero;
86 }
87 
88 struct in6_addr {
89 	union {
90 		uint8_t[16]		__s6_addr;
91 		uint16_t[8]	__s6_addr16;
92 		uint32_t[4]	__s6_addr32;
93 	}
94     alias s6_addr = __s6_addr;
95     alias s6_addr16 = __s6_addr16;
96     alias s6_addr32 = __s6_addr32;
97 }
98 
99 struct sockaddr_in6 {
100 	uint8_t			sin6_len;
101 	sa_family_t		sin6_family;
102 	in_port_t		sin6_port;
103 	uint32_t		sin6_flowinfo;
104 	in6_addr		sin6_addr;
105 	in_port_t		sin6_vport;
106 	uint32_t		sin6_scope_id;
107 }
108 
109 /* Address to accept any incoming messages. */
110 enum INADDR_ANY = (cast(in_addr_t) 0x00000000);
111 
112 /* Address to send to all hosts. */
113 enum INADDR_BROADCAST =(cast(in_addr_t) 0xffffffff);
114 
115 enum INADDR_LOOPBACK =(cast(in_addr_t) 0x7f000001); // 127.0.0.1;
116 enum INADDR_NONE =(cast(in_addr_t) 0xffffffff);
117 
118 auto	IN_CLASSA(uint i) { return ((cast(uint32_t)(i) & 0x80000000) == 0) ;}
119 enum IN_CLASSA_NET = 0xff000000;
120 enum IN_CLASSA_NSHIFT = 24;
121 enum IN_CLASSA_HOST = 0x00ffffff;
122 enum IN_CLASSA_MAX = 128;
123 
124 auto	IN_CLASSB(uint i){return ((cast(uint32_t)(i) & 0xc0000000) == 0x80000000);}
125 enum	IN_CLASSB_NET = 		0xffff0000;
126 enum	IN_CLASSB_NSHIFT = 	16;
127 enum	IN_CLASSB_HOST = 		0x0000ffff;
128 enum	IN_CLASSB_MAX = 		65536;
129 
130 auto	IN_CLASSC(uint i){return ((cast(uint32_t)(i) & 0xe0000000) == 0xc0000000);}
131 enum	IN_CLASSC_NET = 0xffffff00;
132 enum	IN_CLASSC_NSHIFT = 8;
133 enum	IN_CLASSC_HOST = 0x000000ff;
134 
135 auto	IN_CLASSD(uint i){return ((cast(uint32_t)(i) & 0xf0000000) == 0xe0000000);}
136 enum	IN_CLASSD_NET		 = 0xf0000000;	/* These ones aren't really */
137 enum	IN_CLASSD_NSHIFT	 = 28;		/* net and host fields, but */
138 enum	IN_CLASSD_HOST		 = 0x0fffffff;	/* routing needn't know.    */
139 alias	IN_MULTICAST = IN_CLASSD;
140 
141 
142 auto	IN_EXPERIMENTAL(uint i) =>	((cast(uint32_t)(i) & 0xf0000000) == 0xf0000000);
143 auto	IN_BADCLASS(uint i) =>		((cast(uint32_t)(i) & 0xf0000000) == 0xf0000000);
144 
145 
146 enum INADDR_UNSPEC_GROUP	= cast(uint32_t)0xe0000000;	/* 224.0.0.0 */
147 enum INADDR_ALLHOSTS_GROUP	= cast(uint32_t)0xe0000001;	/* 224.0.0.1 */
148 enum INADDR_ALLRTRS_GROUP	= cast(uint32_t)0xe0000002;	/* 224.0.0.2 */
149 enum INADDR_MAX_LOCAL_GROUP	= cast(uint32_t)0xe00000ff;	/* 224.0.0.255 */
150 
151 enum	IN_LOOPBACKNET =		127;			/* official! */
152 
153 // #define ntohs __builtin_bswap16
154 // #define htons __builtin_bswap16
155 // #define ntohl __builtin_bswap32
156 // #define htonl __builtin_bswap32
157 
158 
159 enum SceNetSocketOption {
160 	/* IP */
161 	SCE_NET_IP_HDRINCL              = 2,
162 	SCE_NET_IP_TOS                  = 3,
163 	SCE_NET_IP_TTL                  = 4,
164 	SCE_NET_IP_MULTICAST_IF         = 9,
165 	SCE_NET_IP_MULTICAST_TTL        = 10,
166 	SCE_NET_IP_MULTICAST_LOOP       = 11,
167 	SCE_NET_IP_ADD_MEMBERSHIP       = 12,
168 	SCE_NET_IP_DROP_MEMBERSHIP      = 13,
169 	SCE_NET_IP_TTLCHK               = 23,
170 	SCE_NET_IP_MAXTTL               = 24,
171 	/* TCP */
172 	SCE_NET_TCP_NODELAY             = 1,
173 	SCE_NET_TCP_MAXSEG              = 2,
174 	SCE_NET_TCP_MSS_TO_ADVERTISE    = 3,
175 	/* SOCKET */
176 	SCE_NET_SO_REUSEADDR            = 0x00000004,
177 	SCE_NET_SO_KEEPALIVE            = 0x00000008,
178 	SCE_NET_SO_BROADCAST            = 0x00000020,
179 	SCE_NET_SO_LINGER               = 0x00000080,
180 	SCE_NET_SO_OOBINLINE            = 0x00000100,
181 	SCE_NET_SO_REUSEPORT            = 0x00000200,
182 	SCE_NET_SO_ONESBCAST            = 0x00000800,
183 	SCE_NET_SO_USECRYPTO            = 0x00001000,
184 	SCE_NET_SO_USESIGNATURE         = 0x00002000,
185 	SCE_NET_SO_SNDBUF               = 0x1001,
186 	SCE_NET_SO_RCVBUF               = 0x1002,
187 	SCE_NET_SO_SNDLOWAT             = 0x1003,
188 	SCE_NET_SO_RCVLOWAT             = 0x1004,
189 	SCE_NET_SO_SNDTIMEO             = 0x1005,
190 	SCE_NET_SO_RCVTIMEO             = 0x1006,
191 	SCE_NET_SO_ERROR                = 0x1007,
192 	SCE_NET_SO_TYPE                 = 0x1008,
193 	SCE_NET_SO_NBIO                 = 0x1100,
194 	SCE_NET_SO_TPPOLICY             = 0x1101,
195 	SCE_NET_SO_NAME                 = 0x1102
196 }
197 
198 
199 enum IP_HDRINCL		= SceNetSocketOption.SCE_NET_IP_HDRINCL;
200 enum IP_TOS			= SceNetSocketOption.SCE_NET_IP_TOS;
201 enum IP_TTL			= SceNetSocketOption.SCE_NET_IP_TTL;
202 enum IP_MULTICAST_IF		= SceNetSocketOption.SCE_NET_IP_MULTICAST_IF;
203 enum IP_MULTICAST_TTL	= SceNetSocketOption.SCE_NET_IP_MULTICAST_TTL;
204 enum IP_MULTICAST_LOOP	= SceNetSocketOption.SCE_NET_IP_MULTICAST_LOOP;
205 enum IP_ADD_MEMBERSHIP	= SceNetSocketOption.SCE_NET_IP_ADD_MEMBERSHIP;
206 enum IP_DROP_MEMBERSHIP	= SceNetSocketOption.SCE_NET_IP_DROP_MEMBERSHIP;
207 
208 /*
209  * Argument structure for IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP.
210  */
211 struct ip_mreq {
212 	in_addr imr_multiaddr;	/* IP multicast address of group */
213 	in_addr imr_interface;	/* local IP address of interface */
214 }